package Model;

import java.util.ArrayList;
import java.util.HashMap;
import View.BarChart;
import parser.RequestParser;
import prjClasses.Player;
import prjClasses.Rating;

public class PlayersRating {
	private final String player = "player";
	private final String rating = "rating";
	//private final String user = "user";

	public void createRatingPlayersChart() {
		ArrayList<Object> allPlayers = getAllPlayers();
		int[] victories = new int[allPlayers.size()];
		String[] playersNames = new String[allPlayers.size()];
		for (int i = 0; i < allPlayers.size(); i++) {
			Player player = (Player) allPlayers.get(i);
			int id = player.getOwner();
			playersNames[i] = player.getFirstname() + " "
					+ player.getLastname();// getPlayerNameById(id);
			Rating ratingByPlayer = getRatingById(id);
			victories[i] = ratingByPlayer.getVictory();
		}
		createChart(playersNames, victories);
	}

	private void createChart(String[] playersNames, int[] victories) {
		BarChart chart = new BarChart();
		chart.setTitle("Numbers of wins");
		chart.setNameXAxis("players");
		chart.setNameYAxis("wins");
		chart.showChart(playersNames, victories);
	}

	private HashMap<String, ArrayList<Object>> getObjectMap(String[][] data) {
		RequestParser par = new RequestParser();
		return par.parseRequest(data);
	}

	private ArrayList<Object> getAllPlayers() {
		RequesToService service = new RequesToService();
		HashMap<String, ArrayList<Object>> allObj = getObjectMap(service
				.getRequestAllPlayers());
		return allObj.get(player);
	}

	private Rating getRatingById(int id) {
		RequesToService service = new RequesToService();
		HashMap<String, ArrayList<Object>> allObj = getObjectMap(service
				.getPlayerRatingById(id));
		ArrayList<Object> arrObj = allObj.get(rating);
		if (arrObj.isEmpty())
			System.out.println("[X] ID not found! ID=" + id);
		if (arrObj.size() > 1)
			System.out.println("[X] ID not unique! ID=" + id);
		return (Rating) arrObj.get(0);
	}
/*
	private String getPlayerNameById(int id) {
		RequesToService service = new RequesToService();
		String[][] strUserById = service.getRequestUserById(id);
		HashMap<String, ArrayList<Object>> allObj = getObjectMap(strUserById);
		ArrayList<Object> arrObj = allObj.get(user);
		if (arrObj.isEmpty())
			System.out.println("[X] ID not found! ID=" + id);
		if (arrObj.size() > 1)
			System.out.println("[X] ID not unique! ID=" + id);
		return ((User) arrObj.get(0)).getLogin();
	}
*/
}
